package main

import (
	"github.com/mark3labs/mcp-go/mcp"
	"github.com/mark3labs/mcp-go/server"
)

// RegisterTools 注册所有 Jenkins 工具
func (j *JenkinsMCPServer) RegisterTools(s *server.MCPServer) {
	// 创建作业
	s.AddTool(
		mcp.NewTool("jenkins_create_job",
			mcp.WithDescription("创建新的 Jenkins 作业"),
			mcp.WithObject("job_config",
				mcp.Required(),
				mcp.Description("作业配置信息，包含name、description、type、config等字段"),
			),
		),
		j.handleCreateJob,
	)

	// 构建作业
	s.AddTool(
		mcp.NewTool("jenkins_build_job",
			mcp.WithDescription("构建 Jenkins 作业"),
			mcp.WithString("job_name",
				mcp.Required(),
				mcp.Description("作业名称"),
			),
			mcp.WithObject("parameters",
				mcp.Description("构建参数（可选）"),
			),
			mcp.WithBoolean("wait",
				mcp.Description("是否等待构建完成（可选）"),
			),
		),
		j.handleBuildJob,
	)

	// 获取作业信息
	s.AddTool(
		mcp.NewTool("jenkins_get_job_info",
			mcp.WithDescription("获取 Jenkins 作业信息"),
			mcp.WithString("job_name",
				mcp.Required(),
				mcp.Description("作业名称"),
			),
		),
		j.handleGetJobInfo,
	)

	// 获取构建信息
	s.AddTool(
		mcp.NewTool("jenkins_get_build_info",
			mcp.WithDescription("获取构建信息"),
			mcp.WithString("job_name",
				mcp.Required(),
				mcp.Description("作业名称"),
			),
			mcp.WithNumber("build_number",
				mcp.Required(),
				mcp.Description("构建号"),
			),
		),
		j.handleGetBuildInfo,
	)

	// 获取构建日志
	s.AddTool(
		mcp.NewTool("jenkins_get_build_log",
			mcp.WithDescription("获取构建日志"),
			mcp.WithString("job_name",
				mcp.Required(),
				mcp.Description("作业名称"),
			),
			mcp.WithNumber("build_number",
				mcp.Required(),
				mcp.Description("构建号"),
			),
			mcp.WithNumber("start",
				mcp.Description("开始行数（可选）"),
			),
		),
		j.handleGetBuildLog,
	)

	// 删除作业
	s.AddTool(
		mcp.NewTool("jenkins_delete_job",
			mcp.WithDescription("删除 Jenkins 作业"),
			mcp.WithString("job_name",
				mcp.Required(),
				mcp.Description("作业名称"),
			),
		),
		j.handleDeleteJob,
	)

	// 列出作业
	s.AddTool(
		mcp.NewTool("jenkins_list_jobs",
			mcp.WithDescription("列出所有作业"),
			mcp.WithString("folder",
				mcp.Description("文件夹路径（可选）"),
			),
		),
		j.handleListJobs,
	)

	// 获取系统信息
	s.AddTool(
		mcp.NewTool("jenkins_get_system_info",
			mcp.WithDescription("获取 Jenkins 系统信息"),
		),
		j.handleGetSystemInfo,
	)

	// 更新作业
	s.AddTool(
		mcp.NewTool("jenkins_update_job",
			mcp.WithDescription("更新 Jenkins 作业配置"),
			mcp.WithString("job_name",
				mcp.Required(),
				mcp.Description("作业名称"),
			),
			mcp.WithObject("job_config",
				mcp.Required(),
				mcp.Description("新的作业配置"),
			),
		),
		j.handleUpdateJob,
	)

	// 获取节点信息
	s.AddTool(
		mcp.NewTool("jenkins_get_node_info",
			mcp.WithDescription("获取 Jenkins 节点信息"),
			mcp.WithString("node_name",
				mcp.Description("节点名称（可选，为空则获取所有节点）"),
			),
		),
		j.handleGetNodeInfo,
	)

	// 创建凭据
	s.AddTool(
		mcp.NewTool("jenkins_create_credential",
			mcp.WithDescription("创建 Jenkins 凭据"),
			mcp.WithString("credential_id",
				mcp.Required(),
				mcp.Description("凭据ID"),
			),
			mcp.WithString("type",
				mcp.Required(),
				mcp.Description("凭据类型"),
			),
			mcp.WithObject("config",
				mcp.Required(),
				mcp.Description("凭据配置"),
			),
		),
		j.handleCreateCredential,
	)

	// 获取插件信息
	s.AddTool(
		mcp.NewTool("jenkins_get_plugin_info",
			mcp.WithDescription("获取 Jenkins 插件信息"),
			mcp.WithString("plugin_name",
				mcp.Description("插件名称（可选，为空则获取所有插件）"),
			),
		),
		j.handleGetPluginInfo,
	)

	// 安装插件
	s.AddTool(
		mcp.NewTool("jenkins_install_plugin",
			mcp.WithDescription("安装 Jenkins 插件"),
			mcp.WithString("plugin_name",
				mcp.Required(),
				mcp.Description("插件名称"),
			),
			mcp.WithString("version",
				mcp.Description("插件版本（可选）"),
			),
		),
		j.handleInstallPlugin,
	)

	// 获取队列信息
	s.AddTool(
		mcp.NewTool("jenkins_get_queue_info",
			mcp.WithDescription("获取 Jenkins 构建队列信息"),
		),
		j.handleGetQueueInfo,
	)

	// 重启 Jenkins
	s.AddTool(
		mcp.NewTool("jenkins_restart",
			mcp.WithDescription("重启 Jenkins"),
			mcp.WithBoolean("safe_restart",
				mcp.Description("是否安全重启（可选）"),
			),
		),
		j.handleRestartJenkins,
	)
}
